package com.mall.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mall.ibatis.bean.Function;
import com.mall.ibatis.bean.Operator;
import com.mall.service.FunctionService;

public class FunctionAction extends Action
{

	private static final FunctionService functionService = FunctionService.getInstance();
	
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception{
		
		String type = request.getParameter("type");
		if(type == null || type.trim().length() == 0){
			return load(mapping,form,request,response);
			
		}else if("functionMenu".equals(type)){
			return FunctionMenu(mapping,form,request,response);
		
		}
		
		return null;
		
		
	}
	//系统功能的功能树加载
	private ActionForward FunctionMenu(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String parentID = request.getParameter("dir");
		List<Function> functionList = functionService.selectFunctions(parentID);
		
		request.setAttribute("functionList", functionList);
		return mapping.findForward("menu");
	}
	

	private ActionForward load(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String parentID = request.getParameter("dir");
		HttpSession session = request.getSession();
		Operator op = (Operator) session.getAttribute("operator");
		String or_operator_id = op.getOpID();
		List<Function> functionList = functionService.selectFunctionsRights(parentID,or_operator_id);

		request.setAttribute("functionList", functionList);
		return mapping.findForward("menu");
	}
	
	
	


}
